--[[-----------------------------------------------------]]--
--	Converts a value to a valid SQL string
--	for the specified type
--[[-----------------------------------------------------]]--
sql.ToSQLString			= function( val, tp )

	--	Treat colors separately
	if IsColor( val ) then
		return color.ToHexString( val )
	end

	--	Convert val to string
	local out			= tostring( val )
	
	--	Convert lua types for StringToType
	if tp == "number" then tp = "float"
	elseif tp == "boolean" then tp = "bool" end
	
	--	Convert output string to given type
	out					= util.StringToType( out, tp )
	
	--	Special SQL case: string
	if tp == "string" then
		out				= SQLStr( tostring( out ) )
		
	--	Special SQL case: boolean
	elseif tp == "bool" then
		out				= tostring( BoolToInt( out ) )
		
	--	Other cases
	else
		out				= tostring( out )
	end
	
	--	Return
	return out
end

--[[-----------------------------------------------------]]--
--	Creates columns string for use in select statements
--[[-----------------------------------------------------]]--
sql.GetColumnString		= function( ... )

	--	Create col string
	local colString		= ""
	for _,v in ipairs( {...} ) do
	
		--	Use valid FieldInfo instances only
		if IsValidInstance( v, FieldInfo ) then 
	
			--	Comma separator
			if colString != "" then
				colString	= colString .. ", "
			end
			
			--	Add id to string
			colString		= colString .. v.Field
			
		end
	end

	--	Return column string
	return colString
end

--[[-----------------------------------------------------]]--
--	Creates quoted columns string for use in INSERT statements
--[[-----------------------------------------------------]]--
sql.GetQuotedColumnString	= function( ... )

	--	Create col string
	local colString			= ""
	for _,v in ipairs( {...} ) do
	
		--	Use valid FieldInfo instances only
		if IsValidInstance( v, FieldInfo ) then 
	
			--	Comma separator & quotes
			if colString != "" then
				colString	= colString .. "', '"
			else
				colString	= "'"
			end
			
			--	Add id to string
			colString		= colString .. v.Field
		
		end
	end
	
	--	Add last quote
	if colString != "" then
		colString			= colString .. "'"
	end

	--	Return column string
	return colString
end

--[[-----------------------------------------------------]]--
--	Creates values string for use in insert VALUES statements
--[[-----------------------------------------------------]]--
sql.GetValueString			= function( ... )

	--	Create col string
	local colString			= ""
	for _,v in ipairs( {...} ) do
	
		--	Use valid FieldInfo instances only
		if IsValidInstance( v, FieldInfo ) then 
	
			--	Comma separator
			if colString != "" then
				colString	= colString .. ", "
			end
			
			--	Add id to string
			colString		= colString .. sql.ToSQLString( v.Value, v.Type )
		
		end
	end

	--	Return column string
	return colString
end

--[[-----------------------------------------------------]]--
--	Creates values string for use in insert VALUES statements
--[[-----------------------------------------------------]]--
sql.GetSpecificValueString	= function( cols, ... )

	--	Create col string
	local colString			= ""
	for _,v in ipairs( cols ) do
		
		--	Get the param
		local param			= select( tonumber( v ), ... )
		
		--	Use valid FieldInfo instances only
		if IsValidInstance( param, FieldInfo ) then 
	
			--	Comma separator
			if colString != "" then
				colString		= colString .. ", "
			end
		
			--	Add id to string
			colString		= colString .. param.Value
		
		end
	end

	--	Return column string
	return colString
end

--[[-----------------------------------------------------]]--
--	Creates columns id string for use in select statements
--[[-----------------------------------------------------]]--
sql.GetIdString				= function( ids, ... )

	--	Create id string
	local idString			= ""
	for _,v in ipairs( ids ) do
		
		--	Get the param
		local param			= select( tonumber( v ), ... )
		
		--	Use valid FieldInfo instances only
		if IsValidInstance( param, FieldInfo ) then 
	
			--	Comma separator
			if idString != "" then
				idString		= idString .. ", "
			end
		
			--	Add id to string
			idString		= idString .. param.Field
		
		end
	end

	--	Return column string
	return idString
end

--[[-----------------------------------------------------]]--
--	Creates quoted columns id string for use in INSERT statements
--[[-----------------------------------------------------]]--
sql.GetQuotedIdString		= function( ids, ... )

	--	Create id string
	local idString			= ""
	for _,v in ipairs( ids ) do
		
		--	Get the param
		local param			= select( tonumber( v ), ... )
		
		--	Use valid FieldInfo instances only
		if IsValidInstance( param, FieldInfo ) then
	
			--	Comma separator & quotes
			if idString != "" then
				idString		= idString .. "', '"
			else
				idString		= idString .. "'"
			end
		
			--	Add id to string
			idString		= idString .. param.Field
		
		end
	end
	
	--	Add last quote
	if idString != "" then
		idString			= idString .. "'"
	end

	--	Return column string
	return idString
end

--[[-----------------------------------------------------]]--
--	Creates param string for use in where statements
--[[-----------------------------------------------------]]--
sql.GetParamString			= function( cols, ... )

	--	Create col string
	local colString			= ""
	local param				= nil
	for _,v in ipairs( cols ) do
		
		--	Get the param
		local param			= select( tonumber( v ), ... )
		
		--	Use valid FieldInfo instances only
		if IsValidInstance( param, FieldInfo ) then
	
			--	Comma separator
			if colString != "" then
				colString		= colString .. " AND "
			end
		
			--	Add id to string
			colString		= colString .. param.Field .. " = " .. sql.ToSQLString( param.Value, param.Type )
		
		end
	end

	--	Return column string
	return colString
end

--[[-----------------------------------------------------]]--
--	Creates update string with all parameters
--[[-----------------------------------------------------]]--
sql.GetUpdateString			= function( ... )

	--	Create col string
	local colString			= ""
	local param				= nil
	for _,v in ipairs( {...} ) do
	
		--	Use valid FieldInfo instances only
		if IsValidInstance( v, FieldInfo ) then 
	
			--	Comma separator
			if colString != "" then
				colString	= colString .. ", "
			end
			
			--	Add id to string
			colString		= colString .. v.Field .. " = " .. sql.ToSQLString( v.Value, v.Type )
		
		end
	end

	--	Return column string
	return colString
end

--[[-----------------------------------------------------]]--
--	Creates a generic select statement
--[[-----------------------------------------------------]]--
sql.GetSelect				= function( tab, ids, ... )

	--	Create select part
	local query				= "SELECT " .. sql.GetColumnString( ... )
	
	--	Add the from
	query = query			.. " FROM " .. tab
	
	--	Add where
	query = query			.. " WHERE " .. sql.GetParamString( ids, ... )
	
	--	Return select query
	return query
end

--[[-----------------------------------------------------]]--
--	Creates a generic insert statement
--[[-----------------------------------------------------]]--
sql.GetInsert				= function( tab, ... )

	--	Create table part
	local query				= "INSERT INTO " .. tab
	
	--	Add the columns part
	query = query			.. " ( " .. sql.GetQuotedColumnString( ... ) .. " )"
	
	--	Add the values part
	query = query			.. " VALUES ( " .. sql.GetValueString( ... ) .. " )"
	
	--	Return insert query
	return query
end

--[[-----------------------------------------------------]]--
--	Creates a generic update statement
--[[-----------------------------------------------------]]--
sql.GetUpdate				= function( tab, ids, ... )

	--	Create table part
	local query				= "UPDATE " .. tab .. " SET "
	
	--	Add the values part
	query = query			.. sql.GetUpdateString( ... )
	
	--	Add the where part
	query = query			.. " WHERE " .. sql.GetParamString( ids, ... )
	
	--	Return update query
	return query
end

--[[-----------------------------------------------------]]--
--	Creates a generic delete statement
--[[-----------------------------------------------------]]--
sql.GetDelete				= function( tab, ids, ... )

	--	Create table part
	local query				= "DELETE FROM " .. tab
	
	--	Add where part
	query = query			.. " WHERE " .. sql.GetParamString( ids, ... )
	
	--	Return delete query
	return query
end